package com.qingxin.ui.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.AccordionNode;
import com.qingxin.ui.bean.TreeNode;
import com.qingxin.ui.bean.UICenter;
import com.qingxin.ui.bean.UICenterTab;
import com.qingxin.ui.bean.UICenterTabNode;
import com.qingxin.ui.bean.UILayout;
import com.qingxin.ui.bean.UILayoutLeft;
import com.qingxin.ui.bean.UILayoutCenter;
import com.qingxin.ui.bean.UILeft;
import com.qingxin.ui.bean.UILeftTab;
import com.qingxin.ui.bean.UILeftTabNode;
import com.qingxin.ui.bean.UILoader;
import com.qingxin.ui.bean.UIModule;
import com.qingxin.ui.service.PermissionFilter;
import com.qingxin.ui.service.UIDynamicReplacerImpl;
import com.qingxin.ui.service.UiService;

@Controller
@RequestMapping(value="/ui")
public class UiAction extends BaseAction {
	
	@Autowired
	private UiService uiService;
	
	private UIDynamicReplacerImpl dynamicReplacer = new UIDynamicReplacerImpl();
	
	/**
	 * 获取模块数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getModules.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getModules(HttpServletRequest request){
		List<UIModule> modules = uiService.getModules();
		modules = PermissionFilter.filterModules(modules, uiService, getLoginUser(request));
		return modules;
	}
	
	
	
	/**
	 * 获取菜单标签
	 * @param module
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getLeftTabs.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getLeftTabs(String module, HttpServletRequest request){
		UILayout layout = UILoader.getUILayout(module, dynamicReplacer);
		if(layout==null){
			return null;
		}
		UILayoutLeft layoutLeft = layout.getLayoutLeft();
		List<UILeftTab> menuTabs = layoutLeft.getLeftTabs();
		List<UILeftTabNode> tabs = new ArrayList<>();
		for(UILeftTab menuTab : menuTabs){
			UILeftTabNode tab = menuTab.getMenuTabNode();
			tabs.add(tab);
		}
		UILeft left = new UILeft(layoutLeft.getWidth(), layoutLeft.getHide(), layoutLeft.getKeeptab(), layoutLeft.getType());
		tabs = PermissionFilter.filterLeftTabs(tabs, uiService, getLoginUser(request));
		left.setTabs(tabs);
		
		return left;
		
	}
	
	
	/**
	 * 获取菜单数据
	 * @param module
	 * @param lefttab
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getLeftData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getLeftData(String module, String lefttab, HttpServletRequest request){
		UILayout layout = UILoader.getUILayout(module, dynamicReplacer);
		if(layout==null){
			return null;
		}
		UILayoutLeft layoutLeft = layout.getLayoutLeft();
		UILeftTab tab = layoutLeft.getLeftTab(lefttab);
		if(tab==null){
			return null;
		}
		if("accordion".equalsIgnoreCase(tab.getType())){
			List<AccordionNode> accordionNodes = tab.getAccordionNodes();
			accordionNodes = PermissionFilter.filterAccordionData(accordionNodes, uiService, getLoginUser(request));
			return accordionNodes;
			
		}else if("tree".equalsIgnoreCase(tab.getType())){
			List<TreeNode> treeNodes = tab.getTreeNodes();
			treeNodes = PermissionFilter.filterTreeData(treeNodes, uiService, getLoginUser(request));
			return treeNodes;
		}
		return null;
	}
	
	
	/**
	 * 获取视图标签
	 * @param module
	 * @param lefttab
	 * @param datatype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getCenterTabs.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getCenterTabs(String module, String lefttab, String datatype, HttpServletRequest request){
		UILayout layout = UILoader.getUILayout(module, dynamicReplacer);
		if(layout==null){
			return null;
		}
		UILayoutCenter layoutCenter =layout.getLayoutCenter(datatype, lefttab);
		if(layoutCenter==null){
			return null;
		}
		List<UICenterTab> centerTabs = layoutCenter.getCenterTabs();
		List<UICenterTabNode> tabs = new ArrayList<>();
		for(UICenterTab centerTab : centerTabs){
			UICenterTabNode tab = null;
			if("ref".equalsIgnoreCase(centerTab.getType())){
				String[] ref = centerTab.getUrl().split(":");
				String _datatype = ref[0];
				String _id = ref[1];
				UILayoutCenter _layoutCenter  = layout.getLayoutCenter(_datatype, "");
				UICenterTab _centerTab = _layoutCenter.getCenterTab(_id);
				tab = _centerTab.getCenterTabNode();
				tab.setId(centerTab.getId());
				tab.setCode(centerTab.getCode());
				if(StringUtils.isNotEmpty(centerTab.getName())){
					tab.setName(centerTab.getName());
				}
				if(StringUtils.isNotEmpty(centerTab.getTitle())){
					tab.setTitle(centerTab.getTitle()); 
				}
			}else {
				tab = centerTab.getCenterTabNode();
			}
			
			if(StringUtils.isNotEmpty(tab.getDynamic())){
				if(uiService.isIncludeDynamicCenterTab(tab.getDynamic(), request)){
					tabs.add(tab);
				}
			}else{
				tabs.add(tab);
			}
		}
		UICenter center = new UICenter();
		center.setDatatype(datatype);
		tabs = PermissionFilter.filterCenterTabNodes(tabs, uiService, getLoginUser(request));
		center.setTabs(tabs);
		
		return center;
		
	}
	
	
	/**
	 * 获取视图配置数据
	 * @param module
	 * @param lefttab
	 * @param datatype
	 * @param centertab
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getCenterData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getCenterData(String module, String lefttab, String datatype, String centertab, HttpServletRequest request){
		UILayout layout = UILoader.getUILayout(module, dynamicReplacer);
		if(layout==null){
			return null;
		}
		UILayoutCenter layoutCenter =layout.getLayoutCenter(datatype, lefttab);
		if(layoutCenter==null){
			return null;
		}
		UICenterTab centerTab = layoutCenter.getCenterTab(centertab);
		if(centerTab==null){
			return null;
		}
		if("ref".equalsIgnoreCase(centerTab.getType())){
			String[] ref = centerTab.getUrl().split(":");
			String _datatype = ref[0];
			String _id = ref[1];
			UILayoutCenter _layoutCenter  = layout.getLayoutCenter(_datatype, "");
			UICenterTab _centerTab = _layoutCenter.getCenterTab(_id);
			
			_centerTab = _centerTab.copy();
			_centerTab.setId(centerTab.getId());
			_centerTab.setCode(centerTab.getCode());
			if(StringUtils.isNotEmpty(centerTab.getName())){
				_centerTab.setName(centerTab.getName());
			}
			if(StringUtils.isNotEmpty(centerTab.getTitle())){
				_centerTab.setTitle(centerTab.getTitle());
			}
			centerTab = _centerTab;
		}else {
			centerTab = centerTab.copy();
		}
		
		centerTab = PermissionFilter.filterCenterTab(centerTab, uiService, getLoginUser(request));
		return centerTab;
	}
	
	
	
//	public Object getTableConfig(String module, String datatype, String tab, HttpServletRequest request){
//		
//	}
	
	
	
}
